package com.gexin.model.notice;

import com.gexin.common.utils.SQLUtils;
import com.gexin.model.notice.base.BaseNoticeNewsCommentReply;
import com.gexin.model.weixin.WeixinUser;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * 业务名称:  t_notice_news_comment_reply(资讯评论回复表) 
 */
@SuppressWarnings("serial")
public class NoticeNewsCommentReply extends BaseNoticeNewsCommentReply<NoticeNewsCommentReply> {
	public static final NoticeNewsCommentReply dao = new NoticeNewsCommentReply();

	public List<NoticeNewsCommentReply> findCommentId(Integer commentId) {
		return find("select nncr.*,user.headimgurl,user.username,user.nickname from t_notice_news_comment_reply nncr left join t_weixin_user user on user.id=nncr.user_id  where nncr.comment_id=?",commentId);
	}

	/**
	 * 获取用户所有的未读消息
	 * @param getpage
	 * @param pageSize
	 * @param authUser
	 */
	public Page<NoticeNewsCommentReply> findNotReadReply(int page , int pageSize , WeixinUser authUser) {
		SQLUtils sql = new SQLUtils();
		sql.append("  from t_notice_news_comment_reply  reply ");
		sql.append(" LEFT JOIN t_notice_news_comment comm on comm.id=reply.comment_id ");
		sql.append(" LEFT JOIN t_notice_news news on news.id=comm.news_id ");
		sql.append(" where comm.user_id=?  order by is_read");
		return paginate(page,pageSize,"select reply.*,news.cover_img,news.id as newsId ",sql.toString(),authUser.getId());
	}

	/**
	 * 统计有多少条未读消息
	 */
	public Long countReplyNotReadNumber(WeixinUser authUser) {
		return Db.queryLong("select count(reply.id) from t_notice_news_comment_reply reply  LEFT JOIN t_notice_news_comment comm on comm.id=reply.comment_id  where comm.user_id=? and (reply.is_read<>1 or reply.is_read is null)",authUser.getId());
	}
}
